//
//  ViewController.swift
//  Clock
//
//  Created by Yinyin Zeng on 23/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit
//时钟
class ViewController: UIViewController {
    
    
    //表盘
    let layerClock = CALayer()
    //秒针
    let layerPointer = CALayer()
    //时针
    let layerHour  = CALayer()
    //分针
    let layerMim = CALayer()
    
    //时间Label
    let timeLabel = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //表盘
        layerClock.contents = UIImage(named: "clock")?.cgImage
        layerClock.backgroundColor = UIColor.lightGray.cgColor
        layerClock.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)
        layerClock.position = self.view.center
        layerClock.opacity = 1 //透明度，不设置不显示
        layerClock.cornerRadius = 100
        layerClock.masksToBounds = true
        self.view.layer.addSublayer(layerClock)
        
        
        
        //时针
        layerHour.backgroundColor = UIColor.black.cgColor
        layerHour.bounds =  CGRect(x: 0, y: 0, width: 4, height: 50)
        layerHour.position = layerClock.position
        layerHour.opacity = 1
        layerHour.anchorPoint = CGPoint(x: 0.5, y: 1)
        layerHour.cornerRadius = 3
        layerHour.masksToBounds = true
        self.view.layer.addSublayer(layerHour)
        
        //分针
        layerMim.backgroundColor = UIColor.black.cgColor
        layerMim.bounds =  CGRect(x: 0, y: 0, width: 4, height: 85)
        layerMim.position = layerClock.position
        layerMim.opacity = 1
        layerMim.anchorPoint = CGPoint(x: 0.5, y: 1)
        layerMim.cornerRadius = 3
        layerMim.masksToBounds = true
        self.view.layer.addSublayer(layerMim)
        
        //表盘圆点
        let roundPoint = CALayer()
        roundPoint.backgroundColor = UIColor.red.cgColor
        roundPoint.bounds =  CGRect(x: 0, y: 0, width: 12, height: 12)
        roundPoint.opacity = 1
        roundPoint.position = layerClock.position
        roundPoint.borderWidth = 2.5
        roundPoint.borderColor = UIColor.black.cgColor
        roundPoint.cornerRadius = 6
        roundPoint.masksToBounds = true
        self.view.layer.addSublayer(roundPoint)
        
        
        
        //秒针
        layerPointer.backgroundColor = UIColor.red.cgColor
        layerPointer.bounds = CGRect(x: 0, y: 0, width: 2, height: 95)
        layerPointer.position = layerClock.position
        layerPointer.opacity = 1
        layerPointer.anchorPoint = CGPoint(x: 0.5, y: 0.9)
        layerPointer.cornerRadius = 1
        layerPointer.masksToBounds = true
        self.view.layer.addSublayer(layerPointer)
        
        timeLabel.frame = CGRect(x: 0, y: 0, width: 220, height: 60)
        timeLabel.center = CGPoint(x: self.view.center.x, y: self.view.center.y  + CGFloat(200))
//        timeLabel.textAlignment = .center
        timeLabel.font = UIFont.systemFont(ofSize: 40)
        self.view.addSubview(timeLabel)
        
//        // Timer一秒后才执行
//        _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timeChange), userInfo: nil, repeats: true)
//
//        self.timeChange(isFirst:true)//故，自行执行一次
        
        //显示链接
        let link = CADisplayLink.init(target: self, selector: #selector(timeChange))
        //把显示链接添加到主循环中
        link.add(to: RunLoop.main, forMode: .default)
        
    }


    @objc func timeChange(isFirst:Bool){
        
        
        
        //小时转动的角度
        let hhAngle = CGFloat(2*Double.pi / 12)
        
        //分钟转动的角度
        let mmAngle = CGFloat(2*Double.pi / 60)
        
        
        //一秒钟旋转的角度
        var ssAngle = CGFloat(2*Double.pi / 60)
        if isFirst{
            ssAngle += ssAngle
        }
        //获取时间
        let dateTime = Date()
        //创建一个时间格式化对象
        let dateFormatter = DateFormatter()
        
        //时间的获取另外一种方式 - 日历
//        let currentTime = Calendar.current
//        let currentSs = CGFloat(currentTime.component(Calendar.Component.second, from: dateTime))
    
        //小时
        dateFormatter.dateFormat = "HH"
        let hh = dateFormatter.string(from: dateTime) as NSString
        let hhTimes = CGFloat(hh.floatValue)
        layerHour.transform = CATransform3DMakeRotation(hhAngle*hhTimes, 0, 0, 1)
        
        //分钟
        dateFormatter.dateFormat = "mm"
        let mm = dateFormatter.string(from: dateTime) as NSString
        let mmTimes = CGFloat(mm.floatValue)
        layerMim.transform = CATransform3DMakeRotation(mmAngle*mmTimes, 0, 0, 1)
        
        //秒
        dateFormatter.dateFormat = "ss"
        let ss = dateFormatter.string(from: dateTime) as NSString
        let ssTimes = CGFloat(ss.floatValue)
        
       
        
        //开启事务
        CATransaction.begin()
        //禁用隐式动画
        CATransaction.setDisableActions(true)
        //需要改变layer的属性
        layerPointer.transform = CATransform3DMakeRotation(ssAngle*ssTimes, 0, 0, 1)
        //提交事务
        CATransaction.commit()
        
        dateFormatter.dateFormat = "HH : mm : ss"
        timeLabel.text = dateFormatter.string(from: dateTime)
        
        
    }
}

